package cn.wolfcode.wolf2w.advice;

import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * 统一异常处理
 */
@ControllerAdvice
@ResponseBody
public class LoginExceptionController {

    @ExceptionHandler(LogicException.class)
    public Object logicExp(LogicException e, HttpServletResponse resp) {

        e.printStackTrace();

        resp.setContentType("application/json;charset=utf-8");

        return JsonResult.error(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);

    }

    @ExceptionHandler(RuntimeException.class)
    public Object  runTimeExp(Exception e, HttpServletResponse resp) {

        e.printStackTrace();

        resp.setContentType("application/json;charset=utf-8");

        return JsonResult.defaultError();

    }
}
